<?php

namespace app\apiadmin\model\invitation;

use app\common\utils\Res;
use think\facade\Db;
use think\Model;


class InvitationModel extends Model
{

    /**
     * @author: zwx
     * @Description: 分页查询数据列表
     * @param {*} $map  查询条件
     * @param {*} $offset  分页偏移量
     * @param {*} $limit  每页数量
     * @Date: 2024-05-12 14:53:13
     */
    public static function pageMap($map, $findName, $offset, $limit)
    {

        $data = Db::table('xh_siyu_invitation_user')
            ->alias('y')
            ->field($findName)
            // ->join(['xh_qwuser_1'=>'u'], ' y.external_userid = u.wx_userid','left')
            ->where($map)->limit($offset, $limit)->group('y.liveId,y.planid,y.external_userid')->order('y.time desc')->select()->toArray();

        foreach ($data as $k => $v) {
            $data[$k]['chengjiao_money'] = number_format($v['chengjiao_money'] / 100, 2);
        }

        return $data;
    }

    /**
     * @author: zwx
     * @Description: 根据日期分组要约的人都是哪一天的
     * @param {*} $map
     * @param {*} $findName
     * @Date: 2024-07-29 23:00:37
     */
    public static function usernumbydate($map, $findName){
        $data = Db::table('xh_siyu_invitation_user')
            ->alias('y')
            ->field($findName)
            ->join(['xh_qwuser_1'=>'u'], ' y.external_userid = u.wx_userid','left')
            ->where($map)->group('date(u.addtime)')->order('date desc')->select()->toArray();
        return $data;
    }


    /**
     * @author: zwx
     * @Description: 查询数据总数
     * @param {*} $map 查询条件
     * @Date: 2024-05-12 14:53:43
     */
    public static function pageMapCount($map)
    {
        $data = Db::table('xh_siyu_invitation_user')
            ->alias('y')
            // ->join(['xh_qwuser_1'=>'u'], ' y.external_userid = u.wx_userid')
            ->where($map)->group('y.liveId,y.planid,y.external_userid')->order('y.time desc')->count();

        return $data;

    }


    /**
     * @author: zwx
     * @Description: 查询标签
     * @param {*} $map
     * @Date: 2024-06-27 18:12:50
     */
    public static function querybiaoqian($map, $findName)
    {
        $data = Db::table('xh_qw_tags_1')
            ->alias('t')
            ->field($findName)
            ->where($map)->order('t.order')->select()->toArray();

        return $data;
    }


    /**
     * @author: zwx
     * @Description: 查询标签组
     * @param {*} $map
     * @Date: 2024-06-27 18:12:50
     */
    public static function querybiaoqiangroup($map, $findName)
    {
        $data = Db::table('xh_qw_groups_1')
            ->alias('g')
            ->field($findName)
            ->where($map)->order('g.order')->select()->toArray();

        return $data;
    }

    /**
     * @author: zwx
     * @Description: 查询标签用户信息
     * @param {*} $map
     * @Date: 2024-06-27 18:12:50
     */
    public static function querytagUser($map, $findName)
    {
        $data = Db::table('xh_qw_user_tags_1')
            ->alias('u')
            ->field($findName)
            ->join(['xh_qwuser_1' => 'q'], 'q.wx_userid = u.userid')
            ->where($map)->group('u.zhujiao,u.userid')->order('u.zhujiao desc')->select()->toArray();

        return $data;
    }
    public static function querytagUser1($map, $findName)
    {
        $data = Db::table('xh_qw_user_tags_1')
            ->alias('u')
            ->field($findName)
            ->join(['xh_qwuser_1' => 'q'], 'q.wx_userid = u.userid')
            ->join(['xh_order_code' => 'oc'], 'u.userid = oc.user_id')
            ->join(['xh_order_1' => 'od'], 'od.id = oc.order_id')
            ->where($map)->group('u.zhujiao,u.userid')->order('u.zhujiao desc')->select()->toArray();
        return $data;
    }


    /**
     * @author: zwx
     * @Description: 查询用户信息1
     * @param {*} $map
     * @Date: 2024-06-27 18:12:50
     */
    public static function queryUser($map, $findName)
    {
        $data = Db::table('xh_qwuser_1')
            ->alias('u')
            ->field($findName)
            ->where($map)->group('u.zhujiao,u.wx_unionid')->order('u.zhujiao desc')->select()->toArray();

        return $data;
    }

    /**
     * @author: zwx
     * @Description: 查询用户信息  根据下单时间
     * @param {*} $map
     * @Date: 2024-06-27 18:12:50
     */
    public static function ordertime_queryUser($map, $findName)
    {
        $data = Db::table('xh_order_1')
            ->alias('o')
            ->join(['xh_order_code' => 'c'], 'c.order_id = o.id')
            ->field($findName)
            ->where($map)
            ->order('o.pay_time desc')
            ->group('c.teacher_id,o.wx_union_id')
            ->select()->toArray();

        return $data;
    }

    /**
     * @author: zwx
     * @Description: 保存数据到  邀约信息表
     * @param {*} $map
     * @Date: 2024-06-28 19:07:33
     */
    public static function saveToMsg($map)
    {

        $data = Db::table('xh_siyu_invitation_msg')->insertGetId($map);

        return $data;
    }

    /**
     * @author: zwx
     * @Description: 保存数据到  邀约计划表
     * @param {*} $map
     * @Date: 2024-06-28 19:07:33
     */
    public static function saveToPlan($map)
    {

        $data = Db::table('xh_siyu_invitation_plan')->insertGetId($map);

        return $data;
    }

    /**
     * @author: zwx
     * @Description: 保存数据到  邀约用户信息表
     * @param {*} $map
     * @Date: 2024-06-28 19:07:33
     */
    public static function saveToUser($map)
    {

        $data = Db::table('xh_siyu_invitation_user')->insertAll($map);

        return $data;
    }


    /**
     * @author: zwx
     * @Description: 邀约后更新order_1表的new_yaoyue为1
     * @param {*} $map
     * @Date: 2024-06-28 19:07:33
     */
    public static function updateOrder1($map, $data)
    {

        $data = Db::table('xh_order_1')->where($map)->save($data);

        return $data;
    }


}